package com.teaching.Controller;

import com.alibaba.fastjson.JSONObject;
import com.teaching.Dao.StudentDaoAchieve;
import com.teaching.Dao.TeacherDaoAchieve;
import com.teaching.Enity.Student;
import com.teaching.Enity.Teacher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.io.IOException;
import java.util.List;

@RestController
public class LoginController {

    @PostMapping(value = "/teacherLogin",produces = "application/json;charset=UTF-8")   //教师登录验证
    public boolean teacherLogin(@RequestBody JSONObject jsonObject) throws IOException{
        int t_id = jsonObject.getInteger("t_id");      //获取接收的json格式字符串中的t_id的值
        String t_pwd = jsonObject.getString("t_pwd");  //获取接收的json格式字符串中的t_pwd的值
        TeacherDaoAchieve teacherDaoAchieve = new TeacherDaoAchieve();    //创建数据库操作对象
        List<Teacher> teacherList = teacherDaoAchieve.findTeacher(t_id);  //寻找id为t_id的老师
        if (teacherList.size()==0){                                       //找不到的话，LIST中对象为0返回错误
            return false;
        }
        else {
            if (t_pwd.equals(teacherList.get(0).getPwd())){               //找到的话对比密码正确返回true错误返回false
                return true;
            }
            else {
                return false;
            }
        }
    }

    @PostMapping(value = "/studentLogin",produces = "application/json;charset=UTF-8")    //学生登录验证
    public boolean studentLogin(@RequestBody JSONObject jsonObject) throws IOException{
        int s_id = jsonObject.getInteger("s_id");
        String s_pwd = jsonObject.getString("s_pwd");
        StudentDaoAchieve studentDaoAchieve = new StudentDaoAchieve();
        List<Student> studentList= studentDaoAchieve.findStudent(s_id);
        if (studentList.size()==0){
            return false;
        }
        else {
            if (s_pwd.equals(studentList.get(0).getPwd())){
                return true;
            }
            else {
                return false;
            }
        }
    }

}
